<?php
class SystemLog extends CApplicationComponent
{
	const LOGIN_SUCCESS = 'Successful';
	const LOGIN_FALIED = 'Failed';
	const LOGIN_FAILED_REASON_AUTHY = 'Authy';
	const LOGIN_FAILED_REASON_IPRES = 'IP Restrict';
	const LOGIN_FAILED_REASON_SUSPEND = 'Suspended User';
	const LOGIN_FAILED_REASON_PWD = 'Invalid Password';
	const LOGIN_FAILED_REASON_CAPTCHA = 'Captcha';
	public function recordLogin($id_user, $status = self::LOGIN_SUCCESS, $failReason = '', $ip = null, $now = null)
	{
		if(app()->params['GLOBAL_SETTING']['LOG_SETTING']['LOGIN_ACTIVITY']){
			$now = $now? $now : time();
			$ip = $ip? $ip : app()->request->getUserHostAddress();
			
			$logLogin = new LogLogin();
			$logLogin->id_user = $id_user;
			$logLogin->status = $status;
			$logLogin->fail_reason = $failReason;
			$logLogin->time_date = date('Y-m-d H:i:s', $now);
			$logLogin->ip_long = $ip;
	
			return $logLogin->save();
		}else{
			return false;
		}
	}
	
	const WACC_ADD = 'WHM Account Added';		//
	const WACC_EDIT = 'WHM Account Edited';		//
	const WACC_DELETE = 'WHM Account Deleted';	//
	const WACC_REFRESH = 'WHM Account Data Refreshed';	//
	const WACC_DIRLOGIN = 'WHM Account Direct Login';	//
	const REB_GRA = 'Reboot Graceful';		//
	const REB_FORCE = 'Reboot Forceful';		//
	const CP_CHPWD = 'cPanel Change Password';		//
	const CP_MODIACC = 'cPanel Modify Account';		//
	const CP_BWL = 'cPanel Bandwidth Limit';		//
	const CP_SUSPENDACC = 'cPanel Suspend Account';		//
	const CP_UNSUSPENDACC = 'cPanel Unsuspend Account';	//
	const CP_TERMIACC = 'cPanel Terminate Account';		//
	public function recordWhmAction($actionEvent, $extraInfo = '', $id_account = null, $id_cpanel_accounts = null, $id_user = null, $now = null)
	{
		if(app()->params['GLOBAL_SETTING']['LOG_SETTING']['ACTION_ACTIVITY']){
			$now = $now? $now : time();
			$id_user = $id_user? $id_user : app()->user->getData()->id_user;
			
			$logAction = new LogAction();
			$logAction->action_event = $actionEvent;
			$logAction->extra_info = $extraInfo;
			$logAction->id_account = $id_account;
			$logAction->id_cpanel_accounts = $id_cpanel_accounts;
			$logAction->id_user = $id_user;
			$logAction->time_date = date('Y-m-d H:i:s', $now);
			
			return $logAction->save();
		}else{	// disabled log action
			return false;
		}
	}
	
	const GROUP_ADD = 'Group Added';
	const GROUP_EDIT = 'Group Edited';
	const GROUP_DELETE = 'Group Deleted';
	public function recordGroupAction($actionEvent, $extraInfo = '', $id_group = null, $id_user = null, $now = null)
	{
		if(app()->params['GLOBAL_SETTING']['LOG_SETTING']['ACTION_ACTIVITY']){
			$now = $now? $now : time();
			$id_user = $id_user? $id_user : app()->user->getData()->id_user;
			
			$logAction = new LogAction();
			$logAction->action_event = $actionEvent;
			$logAction->extra_info = $extraInfo;
			$logAction->id_group = $id_group;
			$logAction->id_user = $id_user;
			$logAction->time_date = date('Y-m-d H:i:s', $now);
			
			return $logAction->save();
		}else{	// disabled log action
			return false;
		}
	}
	
	const HOST_ADD = 'Host Added';
	const HOST_EDIT = 'Host Edited';
	const HOST_DELETE = 'Host Deleted';
	const HOST_DIRLOGIN = 'Host Direct Login';
	public function recordHostAction($actionEvent, $extraInfo = '', $id_host = null, $id_user = null, $now = null)
	{
		if(app()->params['GLOBAL_SETTING']['LOG_SETTING']['ACTION_ACTIVITY']){
			$now = $now? $now : time();
			$id_user = $id_user? $id_user : app()->user->getData()->id_user;
			
			$logAction = new LogAction();
			$logAction->action_event = $actionEvent;
			$logAction->extra_info = $extraInfo;
			$logAction->id_host = $id_host;
			$logAction->id_user = $id_user;
			$logAction->time_date = date('Y-m-d H:i:s', $now);
			
			return $logAction->save();
		}else{	// disabled log action
			return false;
		}
	}

	const USER_ADD = 'User Added';
	const USER_EDIT = 'User Edited';
	const USER_DELETE = 'User Deleted';
	const USER_AUTHY_EN = 'User Authy Enabled';
	const USER_AUTHY_DIS = 'User Authy Disabled';
	const USER_INDIVI_UP = 'User Individual Updated';
	public function recordUserAction($actionEvent, $extraInfo = '', $id_user = null, $now = null)
	{
		if(app()->params['GLOBAL_SETTING']['LOG_SETTING']['ACTION_ACTIVITY']){
			$now = $now? $now : time();
			$id_user = $id_user? $id_user : app()->user->getData()->id_user;
			
			$logAction = new LogAction();
			$logAction->action_event = $actionEvent;
			$logAction->extra_info = $extraInfo;
			$logAction->id_user = $id_user;
			$logAction->time_date = date('Y-m-d H:i:s', $now);
			
			return $logAction->save();
		}else{	// disabled log action
			return false;
		}
	}
	
	const ROLE_ADD = 'Role Added';
	const ROLE_EDIT = 'Role Edited';
	const ROLE_DELETE = 'Role Deleted';
	const PER_EDIT = 'Permissions Edited';
	public function recordRoleAction($actionEvent, $extraInfo = '', $id_user_role = null, $id_user = null, $now = null)
	{
		if(app()->params['GLOBAL_SETTING']['LOG_SETTING']['ACTION_ACTIVITY']){
			$now = $now? $now : time();
			$id_user = $id_user? $id_user : app()->user->getData()->id_user;
			
			$logAction = new LogAction();
			$logAction->action_event = $actionEvent;
			$logAction->extra_info = $extraInfo;
			$logAction->id_user_role = $id_user_role;
			$logAction->id_user = $id_user;
			$logAction->time_date = date('Y-m-d H:i:s', $now);
			
			return $logAction->save();
		}else{	// disabled log action
			return false;
		}
	}
	
	const RE_HTTP = 'Restart Web Server';
	const RE_APACHE = 'Restart Apache';
	const RE_MYSQL = 'Restart MySQL';
	const RE_SSH = 'Restart SSH';
	const RE_DNS = 'Restart DNS';
	const RE_FTP = 'Restart FTP';
	const RE_EXIM = 'Restart Exim';
	const RE_IMAP = 'Restart IMAP';
	const RE_POP3 = 'Restart POP3';
	public function recordRestartAction($service, $extraInfo = '', $id_account = null, $id_cpanel_accounts = null, $id_user = null, $now = null)
	{
		if(app()->params['GLOBAL_SETTING']['LOG_SETTING']['ACTION_ACTIVITY']){
			$now = $now? $now : time();
			$id_user = $id_user? $id_user : app()->user->getData()->id_user;
			
			$service_actionEvent = array(
				'httpd' => self::RE_HTTP,
				'mysql' => self::RE_MYSQL,
				'sshd' => self::RE_SSH,
				'named' => self::RE_DNS,
				'ftpd' => self::RE_FTP,
				'exim' => self::RE_EXIM,
				'imap' => self::RE_IMAP,
				'cppop' => self::RE_POP3
			);
			$logAction = new LogAction();
			$logAction->action_event = empty($service_actionEvent[$service])? 'Restart ?' : $service_actionEvent[$service];
			$logAction->extra_info = $extraInfo;
			$logAction->id_account = $id_account;
			$logAction->id_cpanel_accounts = $id_cpanel_accounts;
			$logAction->id_user = $id_user;
			$logAction->time_date = date('Y-m-d H:i:s', $now);
			
			return $logAction->save();
		}else{	// disabled log action
			return false;
		}
	}
}